package com.genesis.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.common.Result;
import com.genesis.mall.dto.PageDto;
import com.genesis.mall.entity.OmsOrderReturnReason;
import com.genesis.mall.service.IOmsOrderReturnReasonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 退货原因表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@RestController
@RequestMapping("/returnReason")
public class OmsOrderReturnReasonController {

    @Autowired
    private IOmsOrderReturnReasonService omsOrderReturnReasonService;

    /**
     * 添加退货原因
     * @param omsOrderReturnReason
     * @return
     */
    @PostMapping("/create")
    public Result save(@RequestBody OmsOrderReturnReason omsOrderReturnReason){

        LambdaQueryWrapper<OmsOrderReturnReason> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OmsOrderReturnReason::getName,omsOrderReturnReason.getName());
        int count = omsOrderReturnReasonService.count(lqw);
        if (count>0){
            return Result.error("原因重复");
        }
        omsOrderReturnReason.setCreateTime(LocalDateTime.now());
        omsOrderReturnReasonService.save(omsOrderReturnReason);
        return Result.success();

    }

    /**
     * 批量删除退货原因
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public Result deleteByIds(@RequestParam List<Long> ids){
        //遍历id
        for (Long id : ids) {
            OmsOrderReturnReason orderReturnReason = omsOrderReturnReasonService.getById(id);
            if (orderReturnReason.getStatus()==1){
                return Result.error("编号"+orderReturnReason.getId()+"正在使用不可删除");
            }
        }
        omsOrderReturnReasonService.removeByIds(ids);
        return Result.success("删除成功");
    }

    /**
     * 分页查询退货原因
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public Result<PageDto<OmsOrderReturnReason>> page(Integer pageNum, Integer pageSize){
        Page<OmsOrderReturnReason> omsOrderReturnReasonPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<OmsOrderReturnReason> lqw = new LambdaQueryWrapper<>();
        lqw.orderByAsc(OmsOrderReturnReason::getSort);
        omsOrderReturnReasonService.page(omsOrderReturnReasonPage,lqw);
        PageDto<OmsOrderReturnReason> omsOrderReturnReasonPageDto = new PageDto<>();
        omsOrderReturnReasonPageDto.setPageNum(pageNum);
        omsOrderReturnReasonPageDto.setPageSize(pageSize);
        omsOrderReturnReasonPageDto.setTotal((int) omsOrderReturnReasonPage.getTotal());
        omsOrderReturnReasonPageDto.setTotalPage((int) omsOrderReturnReasonPage.getPages());
        omsOrderReturnReasonPageDto.setList(omsOrderReturnReasonPage.getRecords());
        return Result.success(omsOrderReturnReasonPageDto);
    }

    /**
     * 修改退货原因启用状态
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/update/status")
    public Result updateStatus(@RequestParam Long ids,@RequestParam Integer status){
        OmsOrderReturnReason omsOrderReturnReason = new OmsOrderReturnReason();
        omsOrderReturnReason.setStatus(status);
        LambdaQueryWrapper<OmsOrderReturnReason> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OmsOrderReturnReason::getId,ids);
        omsOrderReturnReasonService.update(omsOrderReturnReason,lqw);
        return Result.success("修改状态成功");
    }

    /**
     * 修改退货原因
     * @param id
     * @param omsOrderReturnReason
     * @return
     */
    @PostMapping("/update/{id}")
    public Result updateReason(@PathVariable Long id,@RequestBody OmsOrderReturnReason omsOrderReturnReason){


        omsOrderReturnReason.setId(id);
        OmsOrderReturnReason byId = omsOrderReturnReasonService.getById(id);
        if (omsOrderReturnReason.getName().equals(byId.getName())){
            omsOrderReturnReasonService.updateById(omsOrderReturnReason);
            return Result.success("修改成功");
        }
        LambdaQueryWrapper<OmsOrderReturnReason> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OmsOrderReturnReason::getName,omsOrderReturnReason.getName());
        int count = omsOrderReturnReasonService.count(lqw);
        if (count>=1){
            return Result.error("原因重复");
        }
        omsOrderReturnReasonService.updateById(omsOrderReturnReason);
        return Result.success("修改成功");

    }

    /**
     * 获取单个退货原因详情信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<OmsOrderReturnReason> getById(@PathVariable Long id){
        return Result.success(omsOrderReturnReasonService.getById(id));
    }
}
